<?php
class ProductAction extends CommonAction{

	public function index(){
		$this->assign("allCategories",$this->getAllCategories());

		$this->display();
	}

	/**
	 * detail
	 */
	public function detail(){
		$this->assign("allCategories",$this->getAllCategories());
		$productId = $_GET["id"];
		$productModel = D("ProductCategoryMappingView");
		
		if(isset($_GET["category_id"])){
			$condition['category_id'] = array('eq',$_GET["category_id"]);
		}
		$condition['product_id'] = array('eq',$productId);
		
		$products = $productModel->where($condition)->select();
		
		$Dict = D("Dict");
		$Dicts = $Dict->relation(true)->where('code="size"')->findAll();
		
		$this->assign("size",$Dicts[0]["DictMap"]);
		$this->assign("productDetail", $products[0]);
		
		$this->display();
	}


	/**
	 * search(page)
	 */
	public function search(){
		$this->assign("allCategories",$this->getAllCategories());
		if(!isset($_GET["category_id"])){
			$category_id = $_POST["category_id"];
		}else{
			$category_id = $_GET["category_id"];
		}
		if(!isset($_GET["keyword"])){
			$keyword = $_POST["keyword"];
		}else{
			$keyword = $_GET["keyword"];
		}
		$ProductCategoryMapping = D("ProductCategoryMappingView");
		if(!empty($category_id)){
			$condition['category_id'] = array('eq',$category_id);
		}
		if(!empty($keyword)){
			$condition['_string'] = "product_name like '%" . $keyword . "%' or prd.description like '%" . $keyword . "%'" ;
		}
		
		import("@.ORG.Page"); //导入分页类
		$count = $ProductCategoryMapping->where($condition)->count();    //计算总数
		$p = new Page ( $count, 10 );
		if(isset($condition)){
			$list=$ProductCategoryMapping->where($condition)->limit($p->firstRow.','.$p->listRows)->order('pid desc')->select();
		}else{
			$list=$ProductCategoryMapping->limit($p->firstRow.','.$p->listRows)->order('id desc')->select();
		}
		$p->setConfig('header','records');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$page = $p->show ();
		$this->assign("keyword",$keyword);
		$this->assign( "page", $page );
		$this->assign ( "list", $list );
		
		$this->display();
		
	}
}
?>